package com.laivi.basic.module.net;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import com.laivi.basic.common.constants.Constant;
import com.laivi.basic.module.log.Log;
import com.laivi.basic.module.log.Logs;

public class Ftp {
	private static Log log=Logs.getLog(Ftp.class);
	private String server;
	private String user;
	private String passwrod;
	
	private FTPClient ftp;
	
	/**
	 * @param server
	 * @param user
	 * @param passwrod
	 */
	public Ftp(String server, String user, String passwrod) {
		super();
		this.server = server;
		this.user = user;
		this.passwrod = passwrod;
		this.ftp=new FTPClient();
	}
	
	public void open(String code,int port){
		try {
			ftp.connect(this.server,port);
			ftp.setControlEncoding(code);
			ftp.login(this.user, this.passwrod);
			ftp.enterLocalPassiveMode();
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
		} catch (SocketException e) {
			log.error("Connect the "+this.server+" ftp Server error.:"+e.getMessage());
		} catch (IOException e) {
			log.error("Login the ftp server Error!:"+e.getMessage());
		}
	}
	
	public void open(){
		open(Constant.Encode.GBK,21);
	}
	
	public FTPFile[] listFile(String dir){
		try {
			return ftp.listFiles(new String(dir.getBytes(Constant.Encode.GBK),Constant.Encode.ISO));
		} catch (IOException e) {
			log.error("list File error!"+e.getMessage());
		}
		return null;
	}
	
	public FTPFile[] listDir(String dir){
		try {
			return ftp.listDirectories(new String(dir.getBytes(Constant.Encode.GBK),Constant.Encode.ISO));
		} catch (IOException e) {
			log.error("list Dir error!"+e.getMessage());
		}
		return null;
	}
	
	public void save(String dir,String fileName,String local){
		try {
			File localFile=new File(local);
			if(!localFile.exists()){
				localFile.mkdirs();
			}
			FileOutputStream fos=new FileOutputStream(local+fileName);
			String remote=new String((dir+fileName).getBytes(Constant.Encode.GBK),Constant.Encode.ISO);
			ftp.retrieveFile(remote, fos);
		} catch (IOException e) {
			log.error("save File error!"+e.getMessage());
		}
	}
	
	public boolean  mkdir(String dir){
		try {
			return ftp.makeDirectory(dir);
		} catch (IOException e) {
			log.error("make dir error!"+e.getMessage());
		}
		return false;
	}
	
	public void upload(String dir,String local){
		try {
			File parent=new File(local);
			if(parent.isDirectory()){
				for(File file:parent.listFiles()){
					if(file.isFile()){
						FileInputStream fis=new FileInputStream(file);
						mkdir(dir+parent.getName());
						ftp.storeFile(dir+parent.getName()+"/"+file.getName(), fis);
					}else{
						upload(dir+parent.getName()+"/",parent.getAbsolutePath()+"\\"+file.getName());
					}
				}
			}else{
				FileInputStream fis=new FileInputStream(parent);
				ftp.storeFile(dir+parent.getName(), fis);
			}
			
		} catch (FileNotFoundException e) {
			log.error("file not found error!"+e.getMessage());
		} catch (IOException e) {
			log.error("upload error!"+e.getMessage());
		}
	}
	
	public boolean delete(String dir){
		try {
			return ftp.deleteFile(dir);
		} catch (IOException e) {
			log.error("delete error!"+e.getMessage());
		}
		return false;
	}
	
	public boolean rename(String from,String to){
		try {
			return ftp.rename(from,to);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void close(){
		if(ftp!=null){
			try {
				ftp.disconnect();
			} catch (IOException e) {
				log.error("close error!"+e.getMessage());
			}
		}
	}
	
	public static void main(String[] args){
		Ftp ftp=new Ftp("10.102.48.253","laivi","laivi");
		ftp.open();
		String dir="/census/";
		for(FTPFile file:ftp.listFile(dir)){
			for(FTPFile subFile:ftp.listFile(dir+file.getName())){
				ftp.save(dir+file.getName(), subFile.getName(),"d:\\tmp\\local\\"+file.getName()+"\\");
			}
		}
		ftp.upload(dir+"abc/", "d:\\commons-net-3.3\\");
		ftp.close();
	}
}
